package com.baizhi.cmfz.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.cmfz.entity.CmfzGuru;
import com.baizhi.cmfz.mapper.CmfzGuruMapper;

import com.baizhi.cmfz.service.CmfzGuruService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/guru")
public class CmfzGuruController {
    @Autowired
    private CmfzGuruService cgs;

    @Autowired
    private CmfzGuruMapper cgm;

    /**
     * 展示所有信息
     */
    @RequestMapping("/selectAllGuru")
    @ResponseBody
    public Map<String,Object> selectAllGuru(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                              @RequestParam(value = "limit",defaultValue = "5") Integer limit){

        //调用serive层写好的分页查询方法
        Page<CmfzGuru> pi = cgs.selectAllByPage(page, limit);
        //改好layui需要的json格式
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",pi.getTotal());
        map.put("data",pi.getRecords());
        return map;
    }


    /**
     * 上传轮播图图片 此处不再使用虚拟路径 直接上传到项目中去
     */



    /**
     * 添加
     */
    @RequestMapping("/addGuru")
    @ResponseBody
    public void addGuru(CmfzGuru g){
        cgs.insertGuru(g);
    }

    /**
     * 编辑
     */
    @RequestMapping("/updateGuru")
    @ResponseBody
    public void updateGuru(CmfzGuru g){
        cgs.updateGuru(g);
    }

    /**
     *根据id查询该对象
     */
    @RequestMapping("/selectById")
    @ResponseBody
    public CmfzGuru selectById(Integer guruId){
        return cgs.selectById(guruId);
    }


    /**
     * 假删除 在前台页面上不展示 修改展示状态
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public void updateStatus(CmfzGuru g){
        cgs.updateStatus(g);
    }

    /**
     * 导出数据
     */
    @RequestMapping("/export")
    @ResponseBody
    public void exportGuru(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //准备数据
        List<CmfzGuru> guruList = cgm.selectList(null);
        //导出参数对象 标题 表名
        ExportParams exportParams = new ExportParams("上师数据", "gurus");
        //调用导出文件的方法
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CmfzGuru.class, guruList);
        //保存
        //FileOutputStream fileOutputStream = new FileOutputStream("C://Users//huimao//Desktop//上师.xls");
        //workbook.write(fileOutputStream);
        //利用浏览器下载
        response.setHeader("content-disposition","attachment;fileName=guru.xls");
        workbook.write(response.getOutputStream());
    }

    /**
     * 导入数据
     */
    @RequestMapping("/import")
    @ResponseBody
    public Map importGuru(MultipartFile file, HttpServletRequest request) throws Exception {
        //获取流
        InputStream inputStream = file.getInputStream();
        //设置标题所占的行数 表头所占的行数
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);

        Map<String, Object> map = new HashMap<>();
        //获取excle中的数据然后封装到集合中去
        try {
            List<CmfzGuru> list = ExcelImportUtil.importExcel(inputStream, CmfzGuru.class, importParams);
            for (CmfzGuru cmfzGuru : list) {
                System.out.println(cmfzGuru);
                cgs.insertGuru(cmfzGuru);

            }
            map.put("msg",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg",false);
        }
        return map;

    }


}
